﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TaiwuModFramework.Common.Utils {
    public class Drawer<K, V> {

        private readonly Dictionary<K, List<V>> data = new Dictionary<K, List<V>>();


        public int Add(K k, V v) {
            if(data.ContainsKey(k)) {
                data[k].Add(v);
                return data[k].Count - 1;
            }
            var l = new List<V>();
            data.Add(k, l);
            l.Add(v);
            return l.Count - 1;
        }




        public bool ContainsKey(K k) {
            return data.ContainsKey(k);
        }

        public List<K> Keys() {
            return data.Keys.ToList();
        }
        public List<V> Values(K k) {
            if(data.ContainsKey(k)) {
                return data[k];
            }
            var l = new List<V>();
            data.Add(k, l);
            return l;

        }
        public List<V> Values() {
            List<V> l = new List<V>();
            foreach(var k in data.Keys) {
                l.AddRange(data[k]);
            }
            return l;
        }
        public V Value(K k, int index) {
            var l = Values(k);
            if(index >= l.Count) throw new IndexOutOfRangeException();
            return l[index];
        }

        public int Count(K k) {
            return Values(k).Count;
        }
        public int Count() {
            int c = 0;
            foreach(var k in data.Keys) {
                c += data[k].Count;
            }
            return c;
        }

    }
}
